import requests
from bs4 import BeautifulSoup

# 请求地址
url = 'https://www.duitang.com/category/'

# 请求头
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
}
# 表单数据
form_data = {
    'cat': 'wedding'
}

print('正在爬取,请稍等...')
# 发送请求 获取服务器响应
# 请求方式：GET POST PUT PATCH DELETE OPTION
response = requests.get(url=url, headers=headers, params=form_data)

# 输出HTTP状态码
print(response.status_code)
# 输出服务器响应字符串
# print(response.text)
html = response.text
print('页面获取完成,正在解析...')
# 创建解析器
soup = BeautifulSoup(html, 'html.parser')
# 元素查找
# 获取页面中所有的img标签
imgs = soup.find_all('img')
print('解析完成正在下载...')
count = 0
for img in imgs:
    # print(img)
    print(img.get('src'))
    img_file = requests.get(img.get('src'))
    count += 1
    file_name = '图片' + str(count) + '.png'
    with open(file=file_name, mode='wb') as f:
        f.write(img_file.content)
